package com.gaga.console.core.controller.bv;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gaga.common.enums.DelFlag;
import com.gaga.common.path.ApiPath;
import com.gaga.common.result.Result;
import com.gaga.common.utils.FolderSelector;
import com.gaga.console.base.controller.BaseController;
import com.gaga.console.base.entity.PageParams;
import com.gaga.console.base.mybatisplus.LambdaQueryWrapperExt;
import com.gaga.console.core.entity.BvScriptCategory;
import com.gaga.console.core.service.BvScriptCategoryService;
import com.gaga.console.core.service.BvScriptItemService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 前端控制器
 *
 * @author liujia
 * @since 2025-03-27
 */
@RestController
@RequestMapping(ApiPath.ROOT + "/bv/script/category")
public class BvScriptCategoryController extends BaseController {

    @Autowired
    private BvScriptCategoryService bvScriptCategoryService;

    @Autowired
    private BvScriptItemService bvScriptItemService;

    private final FolderSelector folderSelector = new FolderSelector();


    /**
     * 获取列表
     */
    @ApiOperation(value = "查询list")
    @PostMapping("/list")
    public Result list(@RequestBody PageParams<BvScriptCategory> data) {

        LambdaQueryWrapperExt<BvScriptCategory> wraps = new LambdaQueryWrapperExt<>();
        wraps.eq(BvScriptCategory::getDelFlag, DelFlag.OK.getCode());
        data.buildSpecWrap(wraps);

        IPage<BvScriptCategory> page = bvScriptCategoryService.page(data.buildPage(), wraps);

        return Result.ok(data.buildTableResult(page));
    }

    /**
     * 根据详细信息
     */
    @ApiOperation(value = "获取单个记录")
    @GetMapping(value = "/{id}")
    public Result<BvScriptCategory> getEntity(@PathVariable Long id) {
        return Result.ok(bvScriptCategoryService.getById(id));
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存记录")
    @PostMapping("/save")
    public Result save(@Validated @RequestBody BvScriptCategory entity) {
        return toAjaxBoolean(bvScriptCategoryService.saveOrUpdate(entity));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除记录")
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        bvScriptCategoryService.removeCategory(ids);
        return Result.ok();
    }

    @ApiOperation(value = "导入品类", httpMethod = "POST")
    @PostMapping(path = "/importCategory")
    @ResponseBody
    public Result importCategory(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        try {
            bvScriptCategoryService.importCategory(file, request);
        } catch (Exception e) {
            response.setStatus(500);
            return Result.fail("上传失败！");
        }
        return Result.ok("上传成功!");
    }

    @ApiOperation(value = "importSelectFolder")
    @PostMapping("/importSelectFolder")
    public Result testSelectFolder() {
        String selectFolder = folderSelector.selectFolder();
        bvScriptCategoryService.importSelectFolder(selectFolder);

        return Result.ok();
    }
}
